﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfRcp.Controls;
using WpfRcp.Core.Views;
using WpfRcp.Core.Views.Presenters;
using WpfRcp.Helpers;
using WpfRcp.Core.Globals;
using AvalonDockControls.Controls;

namespace AvalonDockControls.Views
{
    /// <summary>
    /// Interaction logic for AvalonDockShell.xaml
    /// </summary>
    public partial class AvalonDockShell : IShell
    {
        public static readonly GridLength SplitterSize = new GridLength(3.0);
        public const double VisualizationMenuMinWidth = 200.0;
        public const double VisualizationMenuMinHeight = 200.0;
        public const double RightMenuMinWidth = 250.0;
        public static readonly GridLength RightMenuDefaultWidth = new GridLength(364.0);
        public const double BottomMenuMinHeight = 25.0;
        public static readonly GridLength BottomMenuDefaultHeight = new GridLength(200.0);

        // Fields
        private GridLength m_PreviousRightMenuWidth = RightMenuDefaultWidth;
        private GridLength m_PreviousBottomMenuHeight = BottomMenuDefaultHeight;

        public AvalonDockShell(IMenuControl control)
        {
            InitializeComponent();

            if (Environment.OSVersion.Version.Major < 6 || GlassHelper.IsGlassEnabled == false)
            {
                ChromeWindowStyle = ChromeWindowStyle.Normal;
                ResizeMode = ResizeMode.NoResize;
                WindowStyle = WindowStyle.None;
                AllowsTransparency = true;
                BorderThickness = new Thickness(1);
            }

            m_HeaderPanel.Children.Add(control as UIElement);
        }

        private void OnBottomTabMenuVisibilityChanged(object sender, VisibilityChangedRoutedEventArgs e)
        {
            if (e.Visibility == Visibility.Visible)
            {
                m_SplitterRow.Height = SplitterSize;
                m_BottomTabMenuRow.Height = m_PreviousBottomMenuHeight;
                m_BottomTabMenuRow.MinHeight = BottomMenuMinHeight;
                m_VerticalGridSplitter.Visibility = Visibility.Visible;
            }
            else
            {
                m_PreviousBottomMenuHeight = m_BottomTabMenuRow.Height;
                m_SplitterRow.Height = new GridLength(0.0);
                m_BottomTabMenuRow.Height = GridLength.Auto;
                m_BottomTabMenuRow.MinHeight = 0.0;
                m_VerticalGridSplitter.Visibility = Visibility.Collapsed;
            }
        }

        #region IShell Members

        public IShellPresentationModel PresentationModel
        {
            set { DataContext = value; }
        }

        public object ViewOwner
        {
            set { throw new ApplicationException("Cannot assign owner to the shell view."); }
        }

        public ITabMenuItem GetTabMenuItemInstance(string regionName)
        {
            switch (regionName)
            {
                case RegionNames.DocumentRegion:
                    return new DocumentContentTabMenuItem();
                case RegionNames.SidePanelRegion:
                    return new DockableContentTabMenuItem();
                case RegionNames.BottomRegion:
                    return new TabMenuItem
                    {
                        IsDetachable = true,
                        DetachedMargin = new Thickness(3.0)
                    };
                default:
                    return null;
            }
        }

        #endregion
    }
}
